package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.product.mapper.SpuImageMapper;
import com.atguigu.gmall.product.service.SpuImageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/***
 * spu图片表service层实现类
 */
@Service
public class SpuImageServiceImpl implements SpuImageService {

    @Autowired
    private SpuImageMapper spuImageMapper;

    /**
     * 保存spu图片表数据
     *
     * @param spuInfoId
     * @param spuImageList
     * @return
     */
    @Override
    public int save(Long spuInfoId, List<SpuImage> spuImageList) {
        int result = 0;
        //1 遍历集合保存
        spuImageList.stream().forEach(spuImage -> {
            //补全squId
            spuImage.setSpuId(spuInfoId);
            //新增数据
            int insert = spuImageMapper.insert(spuImage);
            //判断
            if(insert<= 0){
                throw  new GmallException("新增spu图片表失败",null);
            }
        });

        //2 返回结构,能到这一步表示新增返回正数
        return 1;
    }

    /**
     * 根据spuId查询数据
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuImage> listBySpuId(Long spuId) {
        return spuImageMapper.selectList(new LambdaQueryWrapper<SpuImage>().eq(SpuImage::getSpuId,spuId));
    }
}
